package leetcode101.ch5;

//LC 695
public class MaxAreaOfIsland {
    static int R;
    static int C;
    static int[][] G;

    public int maxAreaOfIsland(int[][] grid) {
        R = grid.length;
        C = grid[0].length;
        G = grid;

        int max = 0;
        for (int r = 0; r < R; r++) {
            for (int c = 0; c < C; c++) {
                max = Math.max(max, dfs(r, c));
            }
        }
        return max;
    }

    private boolean inArea(int r, int c) {
        return r >= 0 && c >= 0 && r < R && c < C;
    }

    private int dfs(int r, int c) {
        if (!inArea(r, c) || G[r][c] == 0) return 0;
        G[r][c] = 0; //沉岛
        return 1 + dfs(r + 1, c) + dfs(r - 1, c)
                + dfs(r, c + 1) + dfs(r, c - 1);
    }


}
